<?php
/**
 * 员工搜索，支持员工姓名和员工编号
 */
apf_require_class('APF_Controller');
apf_require_class('Bll_Hr_User');
apf_require_class('Const_User');
class Hr_Ajax_SearchUserController extends APF_Controller {

    public function handle_request() {
        $params = APF::get_instance()->get_request()->get_parameters();
        $kw = $params['name'];

        $status=0;
        if(empty($kw)){
            $this->json_encode($status,'参数错误');
        }
        if( mb_strlen($kw,'UTF-8') < 2){
            $this->json_encode(1,array());
        }

        $user_bll = new Bll_Hr_User();
        $data=$user_bll->get_search_user_list($kw);
        if(empty($data)){
            $this->json_encode(1,array());
        }
        $result=array();
        foreach($data as $jobid=> $val){
            $name = $val['user_name'].'('.$val['user_code'].')'.' - '.$val['position_name'];
            $result[] = array(
                    'user_id'=>$val['user_id'],
                    'user_code'=>$val['user_code'],
                    'user_name'=>$val['user_name'],
                    'name'=>$name,
                    'job_id'=>$jobid,
                    'department_name'=>$val['bu_name'].$val['department_name'],
                    'position_name'=>$val['position_name'],
                    'p_level'=>Const_User::get_user_degree($val['p_level'],'P'),
                    'm_level'=>Const_User::get_user_degree($val['m_level'],'M'),
                    'code_name'=>$val['user_code'].' - '.$val['user_name'],
                    'city_id'=>$val['city_id'],
                    'enter_date'=>date('Y-m-d',strtotime($val['enter_date'])),
            );
        }
        //search start
        $status = 1;
        $this->json_encode($status,$result);
    }
    /**
     *
     * @param int $status 0错误 1成功
     * @param unknown_type $res
     */
    public function json_encode($status,$res){
        $result = array(
                'errcode'=>$status
        );
        if($status){
            $result['data']=$res;
        }else{
            $result['msg']=$res;
        }
        echo json_encode($result);
        exit();
    }
}
